﻿
//计算字符串长度
function strlen(key) {
    var l = escape(key), len;
    len = l.length - (l.length - l.replace(/\%u/g, "u").length) * 4;
    l = l.replace(/\%u/g, "uu");
    len = len - (l.length - l.replace(/\%/g, "").length) * 2;
    return len;
}

//判断字符串中是否含有特殊字符
function havespecial(key) {
    var regspecial = />|<|,|\[|\]|\{|\}|\?|\/|\+|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i;
    if (regspecial.test(key)) {
        return true;
    }
    else {
        return false;
    }
}

function LTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(0)) != -1) {
        var j = 0, i = s.length;
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {
            j++;
        }
        s = s.substring(j, i);
    }
    return s;
}

function RTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
        var i = s.length - 1;
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {
            i--;
        }
        s = s.substring(0, i + 1);
    }
    return s;
}
//去空格
function trim(str) {
    return RTrim(LTrim(str));
}

//是否为空
function Check_Empty(pObj) {
    var obj = eval(pObj);
    if (obj == null || trim(obj.value) == "") {
        return false;
    }
    return true;
}

//整数或小数
function IsFloat(val) {
    var s = document.getElementById(val);
    if (s.value != '' && (isNaN(s.value) || s.value < -1)) {
        return false;
    }
    return true;
}

//是否为数字
function IsInt(o) {
    var s = document.getElementById(o);
    var reg = /^[0-9]*$/;
    if (!reg.test(trim(s.value))) {
        return false;
    }
    else {
        return true;
    }
}

//邮箱地址验证
function Check_Email(strEmail) {
    /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
    var emailReg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
    if (emailReg.test(strEmail)) {
        return true;
    }
    else {
        return false;
    }
}

//验证url
function Check_Url(strUrl) {
    var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
        + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?"
        + "(([0-9]{1,3}\.){3}[0-9]{1,3}"
        + "|"
        + "([0-9a-z_!~*'()-]+\.)*"
        + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\."
        + "[a-z]{2,6})"
        + "(:[0-9]{1,4})?"
        + "((/?)|"
        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
    var re = new RegExp(strRegex);
    if (re.test(strUrl)) {
        return true;
    }
    else {
        return false;
    }
}

//至少一个选中
function check_checked(pObj, errMsg) {
    var obj = eval(pObj);
    if (obj == null || obj.length <= 0) {
        if (errMsg == null || errMsg == "")
        { alert("选择为空!"); }
        else
        { alert(errMsg); }

        obj.focus();
        return false;
    }
    else {
        var check_temp = false;
        for (var i = 0; i < obj.length; i++) {
            if (obj[i].checked)
            { check_temp = true; break; }
        }
        if (!check_temp) {
            if (errMsg == null || errMsg == "")
            { alert("选择为空!"); }
            else
            { alert(errMsg); }
            return check_temp;
        }
    }
    return true;
}

//检查页面元素的自定义属性
function checkAttr(obj) {
    for (i = 0; i < obj.length; i++) {
        try {
            if (obj[i].getAttribute("need") != null && trim(obj[i].value) == "") {
                alert(obj[i].getAttribute("need"));
                obj[i].focus();
                return false;
            }
            if (obj[i].getAttribute("int") != null && trim(obj[i].value) != "" && !IsInt(obj[i].id)) {
                alert(obj[i].getAttribute("int"));
                obj[i].focus();
                return false;
            }
            if (obj[i].getAttribute("float") != null && !IsFloat(obj[i].id)) {
                alert(obj[i].getAttribute("float"));
                obj[i].focus();
                return false;
            }
            if (obj[i].getAttribute("email") != null && obj[i].value != "" && !Check_Email(obj[i].value)) {
                alert(obj[i].getAttribute("email"));
                obj[i].focus();
                return false;
            }
        }
        catch (e)
        { return false; }
    }
    return true;
}
//验证字符串最大长度
function isOutside(str, len) {
    if (str.replace(/[^\x00-\xff]/g, "rr").length > len) {
        return true;
    }
    else {
        return false;
    }
}
//验证字符串长度范围
function isOutside2(str, minlen, maxlen) {
    if (str.replace(/[^\x00-\xff]/g, "rr").length >= minlen && str.replace(/[^\x00-\xff]/g, "rr").length <=
maxlen) {
        return true;
    }
    else {
        return false;
    }
}
//获取对象
function oo(objId) {
    return document.getElementById(objId);
}

//自动将页面的第一个表单(form)的第一个非readonly和非disabled的input输入框置为输入焦点     
function getFocus() {
    if (document.forms.length > 0 && document.forms[0].elements.length > 0) {
        for (var i = 0; i < document.forms[0].elements.length; i++) {
            if (document.forms[0].elements[i].type == "text" && document.forms[0].elements[i].readOnly != true &&
document.forms[0].elements[i].disabled != true) {
                document.forms[0].elements[i].focus();
                break;
            }
        }
    }
}

//判断是否是正确的日期格式
function isdate(yyyy, mm, dd) {
    var y = yyyy.value;
    var m = mm.value;
    var d = dd.value;
    var reg = /^[0-9]*$/;
    if (!reg.test(y)) {
        alert("请填写正确的日期");
        yyyy.focus();
        return false;
    }
    if (!reg.test(m)) {
        alert("请填写正确的日期");
        mm.focus();
        return false;
    }
    if (!reg.test(d)) {
        alert("请填写正确的日期");
        dd.focus();
        return false;
    }
    if (y > 2099 || y < 1999) {
        alert("请填写正确的年份");
        yyyy.focus();
        return false;
    }

    if (m < 1 || m > 12) {
        alert("请填写正确的月份");
        mm.focus();
        return false;
    }
    if (d < 1 || d > 31) {
        alert("请填写正确的日期");
        dd.focus();
        return false;
    }
    switch (d) {
        case "29":
            //如果是2月份
            if (m == "2" || m == "02") {
                //        if( (y / 100) * 100 == y && (y / 400) * 400 != y)
                //        {
                //          //如果年份能被100整除但不能被400整除 (即闰年)
                //          return true;
                //        }
                if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))//是否为闰年
                {
                    return true;
                }
                else {
                    alert("请填写正确的日期");
                    dd.focus();
                    return false;
                }
            }
            break;
        case "30":
            //2月没有30日
            if (m == "2") {
                alert("请填写正确的日期");
                dd.focus();
                return false;
            }
            break;
        case "31":
            //2、4、6、9、11月没有31日
            if (m == "2" || m == "4" || m == "6" || m == "9" || m == "11" || m == "02" || m == "04" || m == "06" ||
m == "09") {
                alert("请填写正确的日期");
                dd.focus();
                return false;
            }
            break;
        default:
    }
    return true;
}

//初始化、指定处理函数、发送请求的函数
function DefineRequest() {
    //开始初始化XMLHttpRequest对象
    var name = false;
    if (window.XMLHttpRequest) {
        //Mozilla 浏览器
        name = new XMLHttpRequest();
        if (name.overrideMimeType) {
            //设置MiME类别
            name.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject) {
        var arrSignatures =
['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
        for (var i = 0; i < arrSignatures.length; i++) {
            try {
                name = new ActiveXObject(arrSignatures[i]);
            }
            catch (e) {
            }
        }
    }
    if (!name) {
        // 异常，创建对象实例失败
        window.alert("不能创建XMLHttpRequest对象实例.");
        return false;
    }
    return name;
}
function check_float(objname, tipname) {
    var tempname;
    if (!tipname) { tempname = objname + "_tipo"; } else { tempname = tipname; }
    if (!IsFloat(objname)) {
        document.getElementById(tempname).style.display = "";
        document.getElementById(objname).focus();
        return false;
    }
    else {
        document.getElementById(tempname).style.display = "none";
        return true;
    }
}
function check_num(objname, tipname) {
    var tempname;
    if (!tipname) { tempname = objname + "_tipo"; } else { tempname = tipname; }
    var dd = /^(\-?\d+)?$/;
    if (!dd.test(document.getElementById(objname).value)) {
        document.getElementById(tempname).style.display = "";
        document.getElementById(objname).focus();
        return false;
    }
    else {
        document.getElementById(tempname).style.display = "none";
        return true;
    }
}
function check_string(objname, tipname) {
    var tempname;
    if (!tipname) { tempname = objname + "_tip"; } else { tempname = tipname; }
    if (trim(document.getElementById(objname).value) == "") {
        document.getElementById(tempname).style.display = "";
        document.getElementById(objname).focus(); return false;
    }
    else { document.getElementById(tempname).style.display = "none"; return true; }
}
//设置page中的值
function SetValue(type, objName, ovalue) {
    type = type.toLowerCase()
    if (type == "group") {
        var objList = document.getElementsByName(objName);
        var len = objList.length;
        for (var i = 0; i < len; i++) {
            if (ovalue.indexOf(objList[i].value) > -1)
                objList[i].checked = true;
        }
    }
    else if (type == "single") {
        var objList = document.getElementsByName(objName);
        var len = objList.length;
        for (var i = 0; i < len; i++) {
            if (objList[i].value == ovalue) {
                objList[i].checked = true;
                break;
            }
        }
    }
    else if (type == "selectchild") {
        var objList = document.getElementById(objName);
        var len = objList.length;
        for (var i = 0; i < len; i++) {
            if (objList.options[i].value == ovalue) {
                objList.options[i].selected = true;
                break;
            }
        }
    }
    else if (type == "all") {
        var objList = document.getElementsByName(objName);
        var len = objList.length;
        for (var i = 0; i < len; i++) {
            objList[i].checked = true;
        }
    }
    return false;
}